package com.timmy.chi.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.timmy.chi.R;
import com.timmy.chi.model.beans.HouTai;
import com.timmy.chi.ui.activity.HouTaiActivity;

import java.util.ArrayList;
import java.util.List;

public class HouTaiAdapter extends BaseAdapter {


    private final HouTaiActivity mContext;

    private List<HouTai.OrdersBean.FoodStatesBean> mList;

    public HouTaiAdapter(HouTaiActivity houTaiActivity) {
        mContext = houTaiActivity;
        mList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public HouTai.OrdersBean.FoodStatesBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public void setList(List<HouTai.OrdersBean.FoodStatesBean> list) {
        mList.addAll(0, list);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final Viewholder viewholder;

        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.houtai_item, null);
            viewholder = new Viewholder();

            viewholder.tv_state = (TextView) view.findViewById(R.id.tv_state);
            viewholder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            viewholder.tv_count = (TextView) view.findViewById(R.id.tv_count);
            view.setTag(viewholder);

        } else {
            viewholder = (Viewholder) view.getTag();
        }

        HouTai.OrdersBean.FoodStatesBean bean = mList.get(position);

        HouTai.OrdersBean.FoodStatesBean.FoodBean food = bean.getFood();
        String name = food.getName();
        int sid = bean.getSid();

        viewholder.tv_name.setText(name);
        viewholder.tv_state.setText(text(sid));
        viewholder.tv_count.setText("x" + bean.getCount());

        return view;
    }

    public void cleaData() {
        mList.clear();
        notifyDataSetChanged();
    }

    class Viewholder {
        TextView tv_name;
        TextView tv_count;
        TextView tv_state;
    }

    private String text(int i) {
        if (i == 1) {
            return "尚未烹饪";
        }else if (i == 3) {

            return "已撤销";
        }

        return "已烹饪";
    }
}
